<?if(!$arParams['IS_AJAX']):?>
<script type="text/javascript">
<!--//<![CDATA[
$().ready(function() {
	$("form[name='ADD_MESSAGE'] input[type='submit']").click(function(){
		var obj = $(this).parents("form");
		var message = $(obj).find("input[name='MESSAGE']").val();
		var params = $(obj).serialize();
		
		if(message.length > 0)
		{
			$(this).attr("disabled", "disabled");
			$.ajax({
				type: "POST",
				//url: "/",
				data: "<?=AJAX_CALL_VARIABLE?>=Y&<?=AJAX_ID_VARIABLE?>=<?=$arParams['AJAX_ID']?>&"+params,
				success: function(msg){
					eval(msg);
				}
			});
		}
		return false;
	});
	$("#online").button().click(function() {
		var options;
		if ($(this).attr("checked") === true) {
			options = {
				label: 'chat online'
			};
		} else {
			options = {
				label: 'chat offline'
			};
		}
		$(this).button('option', options);
	});
	$("#hideusers").button().click(function() {
		var options;
		if ($(this).attr("checked") === true) {
			options = {
				label: 'show users'
			};
			$("div.users").parent("td").show();
		} else {
			options = {
				label: 'hide users'
			};
			$("div.users").parent("td").hide();
		}
		$(this).button('option', options);
	});
	
	<?if($arResult['USER']['ID']):?>
		OpenChat('<?=$arResult['USER']['ID']?>');
		AddMessage('<?=date("H:i:s")?>', '', 'К нам присоединился <?=$arResult['USER']['LOGIN']?>');
		AddMessage('<?=date("H:i:s")?>', '', 'Приветствую тебя, странник!');
	<?endif;?>

	RefreshChat();
	
	$(window).resize(function(){
		ChatResize();
	});
	
//	$('#switcher').themeswitcher();
//	$("#online").xOpenChat();

});
function InitChat(id)
{
	if(id)
	{
		$("input[name='LAST_UPDATE_ID']").val(id);
		$("input[name='MESSAGE']").val("");
		$("form[name='ADD_MESSAGE'] input[type='submit']").attr("disabled", "");
	}
}

function ChatResize()
{
	var window_height = $(window).height();
	var window_width  = $(window).width();
	
	$("div.messages").css("height", window_height-110+'px').scrollTop($('div.messages').height()+10);
	$("div.users").css("height", window_height-110+'px');
	$("#message").css("width", window_width-70+'px');
}

function ChatSetOnline()
{
	$("#online").attr("checked", "checked");
}

function ChatSetOffline()
{
	$("#online").attr("checked", "");
}

function ShowError(text)
{
	$("div.autherror").html(text);
}

function AddMessage(date, user, text)
{
	$('div.messages').append('<div><span class="md">'+date+'</span> <span class="mu">'+user+'</span> <span class="mt">'+text+'</span></div>');
	$('div.messages').scrollTop($('div.messages').height()+10);
	
	//CleanMessages(<?=$arParams['MAX_MESSAGES_IN_CHAT']?>);
}

function CleanMessages(max_count_messages)
{
	var count_messages = $("div.messages").children("div").size();
	
	if(count_messages >= max_count_messages)
	{
		count = count_messages - max_count_messages;
		var c = 0;
		$("div.messages").children("div").each(function(){
			if(c < count)
			{
				$(this).remove();
			}c++;
		})
	}
}

function RefreshChat()
{
	var refresh_timeout = $("#timeout").children("option:selected").val();//alert(refresh_timeout);
	refresh_timeout = refresh_timeout ? refresh_timeout : '5000';//alert(refresh_timeout);
	
	if($("#online").attr("checked") === true)
	{
		var last_update_id = $("input[name='LAST_UPDATE_ID']").val();//alert(last_update_id);
		$.ajax({
			type: "POST",
			//url: "",
			data: "<?=AJAX_CALL_VARIABLE?>=Y&<?=AJAX_ID_VARIABLE?>=<?=$arParams['AJAX_ID']?>&ACTION=REFRESH_CHAT&LAST_UPDATE_ID="+last_update_id,
			success: function(msg){
				eval(msg);
			}
		});
	}
	ChatResize();
	setTimeout(RefreshChat, refresh_timeout)
}
//]]>-->
</script>

<?CMain::ShowArrayError($arResult['ERROR'])?>

<table cellpadding="0" cellspacing="0" width="100%" height="100%" border="1" id="chat">
	<tr valign="top">
		<td id="messages-td">
			<div class="messages"></div>		
		</td>
		<td width="150" id="users-td">
			<div class="users">
				<div class="user" id="1">Admin</div>
				<?for($i=4;$i<20;$i++):?>
				<div class="user" id="<?=$i?>">Странник #<?=$i?></div>
				<?endfor;?>
			</div>		
		</td>
	</tr>
	<tr valign="top">
		<td id="send-td" colspan="2" height="100">
			<div class="send">
				<?if(CUser::IsAuthorized()):?>
					<form action="" name="ADD_MESSAGE">
						<input type="hidden" name="USER_ID" value="<?=$arResult['USER']['ID']?>" />
						<input type="hidden" name="ACTION" value="ADD_MESSAGE" />
						<table cellpadding="0" cellspacing="0">
							<tr>
								<td><input type="text" name="MESSAGE" maxlength="140" value="" id="message" /></td>
								<td><input type="submit" value="Send" id="button-send-message" /></td>
							</tr>
						</table>
					</form>
				<?endif;?>				
				<form action="" name="OPTIONS">
					<input type="hidden" name="LAST_UPDATE_ID" value="0" />
					<input type="checkbox" name="ONLINE" value="Y" id="online" checked="checked" /><label for="online">chat online</label> | 
					<input type="checkbox" name="HIDE_USERS" value="Y" id="hideusers" checked="checked" /><label for="hideusers">show users</label> | 
					<select name="TIMEOUT" id="timeout">
						<option value="1000">Update 1 секунда</option>
						<option value="5000" selected="selected">Update 5 секунд</option>
						<option value="10000">Update 10 секунд</option>
						<option value="20000">Update 20 секунд</option>
						<option value="30000">Update 30 секунд</option>
						<option value="60000">Update 1 минута</option>
					</select>
				</form>
				<!--<script type="text/javascript" src="http://jqueryui.com/themeroller/themeswitchertool/"></script>-->
				<!--<div id="switcher"></div>-->
			</div>
		</td>
	</tr>
</table>



<?else:?>
	<?if($arResult['ACTION'] == 'REFRESH_CHAT'):?>
		<?if(count($arResult['MESSAGES'])):?>
			<?foreach ($arResult['MESSAGES'] as $arMessage):?>
			AddMessage('<?=$arMessage['DATE_CREATE']?>', '<?=$arMessage['USER_NAME']?>', '<?=$arMessage['MESSAGE']?>');
			<?endforeach;?>
			InitChat('<?=$arResult['MESSAGES'][count($arResult['MESSAGES'])-1]['ID']?>');
		<?endif;?>
	<?elseif($arResult['ACTION'] == 'ADD_MESSAGE'):?>
		//InitChat('<?=$arResult['TIME']?>');
	<?else:?>
		Hello 
	<?endif;?>
<?endif;?>